$(function () {
    var selectCompanyList


    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer

        form.on('submit(chooseedit)', function (choosedata) {
            $('#chooseedit').css("display","none")

            if (choosedata.field.chooseadd == 0){
                $('#addcompany').css("display","block")
            }else{
                $('#addproject').css("display","block")
                $.ajax({
                    url: '/getAllCompanyName',
                    type: 'get',
                    success: function (data) {
                        selectCompanyList = data
                        addOptions(selectCompanyList, "company")
                        form.render()
                    }
                })
            }

            return false
        })


        $("button[name='returnlaststep']").click(function () {
            $('#addcompany').css("display","none")
            $('#addproject').css("display","none")
            $('#chooseedit').css("display","block")
        })

        form.on('submit(addcompany)', function (adddata) {
            $.ajax({
                url:'checkCompanyName',
                type:'get',
                data:{
                    companyname:adddata.field.company
                },
                success:function (countdata) {
                    if (countdata == 0){
                        $.ajax({
                            url:'addCompany',
                            type: 'post',
                            data:{
                                companyname: adddata.field.company
                            },
                            success:function () {
                                $.ajax({
                                    url:'insertProject',
                                    type: 'post',
                                    data:{
                                        companyname: adddata.field.company,
                                        projectname: adddata.field.project
                                    },
                                    success:function () {
                                        layer.msg("添加成功")
                                        setTimeout(function () {
                                            window.location.reload();
                                        }, 1000)
                                    }
                                })
                            }
                        })
                    }else {
                        layer.msg("该企业已存在，请重新输入或返回上一步！")
                    }
                }
            })
            return false
        })

        form.on('submit(addproject)', function (adddata) {
            $.ajax({
                url:'checkProjectName',
                type:'get',
                data:{
                    companyname:adddata.field.company,
                    projectname:adddata.field.project
                },
                success:function (countdata) {
                    if (countdata == 0){
                        $.ajax({
                            url:'insertProject',
                            type: 'post',
                            data:{
                                companyname: adddata.field.company,
                                projectname: adddata.field.project
                            },
                            success:function () {
                                layer.msg("添加成功，您可以继续添加")

                            }
                        })
                    }else {
                        layer.msg("该企业以存在该工程项目，请重新输入！")
                    }
                }
            })
            return false
        })
    });
    function addOptions(data, str) {
        var s = "#" + str
        var pro = $(s);
        pro.empty()
        var options = '<option value="" selected=""></option>';
        for (var i = 0; i <= data.length-1; i++) {
            options += '<option value="' + data[i] + '" >' + data[i] + '</option>';
        }
        pro.append(options);
    }
})